package edu.cdbi.view;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.util.LinkedHashMap;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

@SuppressWarnings("serial")
public abstract class BasicPanel extends JPanel {
	
	protected final static short STATE_ADD = 0;
	protected final static short STATE_MODIFY = 1;
	protected final static short STATE_VIEW = 2;
	
//	protected LinkedHashMap<String, BasicComponent> modifyComponents;
	protected LinkedHashMap<String, BasicComponent> addComponents;
	
	protected short state = STATE_ADD;
	
	protected JPanel topToolBar;
	protected JButton addButton;
	protected JButton viewButton;
//	protected JButton modifyButton;
	
	protected JPanel centerContext;
	protected CardLayout centerCardLayout;
	protected JScrollPane modifyPanel;
	protected JScrollPane addPanel;
	protected JPanel innerModifyPanel;
	protected JPanel innerAddPanel;
	protected JScrollPane viewPanel;
	protected DefaultTableModel DTM;
	protected JTable table;
	protected JPopupMenu popupMenu;
	
	protected JPanel buttonConfirmatoryBar;
	protected JButton buttonOK;
	protected JButton buttonCancel;
	
	public BasicPanel(){
//		modifyComponents = new LinkedHashMap<String, BasicComponent>();
		addComponents = new LinkedHashMap<String, BasicComponent>();
		initialData();
		initialButton();
		initialView();
		initialDataView();
		
		setLayout(new BorderLayout());
		add(topToolBar, BorderLayout.NORTH);
		add(centerContext, BorderLayout.CENTER);
		add(buttonConfirmatoryBar, BorderLayout.SOUTH);
	}
	
	protected abstract void initialData();
	
	private void initialDataView(){
		innerAddPanel.setLayout(null);
		for(int i = 0; i < addComponents.size(); i++){
			JLabel label = new JLabel((String) addComponents.keySet().toArray()[i]);
			label.setHorizontalAlignment(JLabel.RIGHT);
			label.setBounds(10, i * 40 + 10, 80, 30);
			innerAddPanel.add(label);
			Container temp = addComponents.get(label.getText()).getContainer();
			temp.setBounds(100, i * 40 + 10, 600, 30);
			innerAddPanel.add(temp);
		}
		innerAddPanel.setPreferredSize(new Dimension(750, addComponents.size() * 40 + 10));
		
//		innerModifyPanel.setLayout(null);
//		for(int i = 0; i < modifyComponents.size(); i++){
//			JLabel label = new JLabel((String) modifyComponents.keySet().toArray()[i]);
//			label.setHorizontalAlignment(JLabel.RIGHT);
//			label.setBounds(10, i * 40 + 10, 80, 30);
//			innerModifyPanel.add(label);
//			Container temp = modifyComponents.get(label.getText()).getContainer();
//			temp.setBounds(100, i * 40 + 10, 600, 30);
//			innerModifyPanel.add(temp);
//		}
//		
//		innerModifyPanel.setPreferredSize(new Dimension(750, modifyComponents.size() * 40 + 10));
		
		
	}
	
	private void initialView(){
		topToolBar = new JPanel();
		topToolBar.setLayout(new FlowLayout(FlowLayout.CENTER));
		topToolBar.add(addButton);
//		topToolBar.add(modifyButton);
		topToolBar.add(viewButton);
		
		buttonConfirmatoryBar = new JPanel();
		buttonConfirmatoryBar.setLayout(new FlowLayout(FlowLayout.CENTER));
		buttonConfirmatoryBar.add(buttonOK);
		buttonConfirmatoryBar.add(buttonCancel);
		
		
		centerContext = new JPanel();
		centerCardLayout = new CardLayout();
		centerContext.setLayout(centerCardLayout);
		addPanel = new JScrollPane();
		addPanel.setAutoscrolls(true);
		innerAddPanel = new JPanel();
		addPanel.setViewportView(innerAddPanel);
		modifyPanel = new JScrollPane();
		modifyPanel.setAutoscrolls(true);
		innerModifyPanel = new JPanel();
		modifyPanel.setViewportView(innerModifyPanel);
		DTM = new DefaultTableModel(){
	        @Override
	        public boolean isCellEditable(int rowIndex, int columnIndex){
	            return false;
	        }
	    };
	    table = new JTable();
	    table.setModel(DTM);
	    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		viewPanel = new JScrollPane();
		viewPanel.setAutoscrolls(true);
		viewPanel.setViewportView(table);
		
		centerContext.add("add", addPanel);
		centerContext.add("modify", modifyPanel);
		centerContext.add("view", viewPanel);
		
		popupMenu = new JPopupMenu();
		JMenuItem mi = new JMenuItem("delete");
		popupMenu.add(mi);
		mi.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent evt) {
				deleteAction();
			}
		});
		
		table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                MousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                MouseReleased(evt);
            }
        });
	}

	private void MousePressed(MouseEvent e) {
		if ((e.getModifiers() & MouseEvent.BUTTON3_MASK) != 0) {
			//按下右鍵
			int modifiers = e.getModifiers();
			//原始按鍵碼 去掉 右鍵碼 加上 左鍵碼 = 只按左鍵
			//也就是把右鍵變成相當於左鍵  這東西在設計TABLE方面有用
			modifiers -= MouseEvent.BUTTON3_MASK;
			modifiers |= MouseEvent.BUTTON1_MASK;
			//製造滑鼠事件
			MouseEvent me = new MouseEvent(e.getComponent(), e.getID(), e
					.getWhen(), modifiers, e.getX(), e.getY(), e
					.getClickCount(), false);
			//給FRAME 傳送 滑鼠事件
			this.dispatchEvent(me);
		}
	}

	private void MouseReleased(MouseEvent evt) {
    	if(evt.isPopupTrigger()){
    		popupMenu.show(this, evt.getX(), evt.getY());
    	}
	}

	private void initialButton() {
		addButton = new JButton("新增");
		viewButton = new JButton("檢視");
//		modifyButton = new JButton("修改");
		buttonOK = new JButton("OK");
		buttonCancel = new JButton("Cancel");
		
		addButton.setSize(80, addButton.getHeight());
		viewButton.setSize(80, addButton.getHeight());
//		modifyButton.setSize(80, addButton.getHeight());
		buttonOK.setSize(80, addButton.getHeight());
		buttonCancel.setSize(80, addButton.getHeight());
		
		addButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				state = STATE_ADD;
				centerCardLayout.show(centerContext, "add");
				addAction();
			}
		});
		
		viewButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				state = STATE_VIEW;
				centerCardLayout.show(centerContext, "view");
				viewAction();
			}
		});
		
//		modifyButton.addActionListener(new ActionListener() {
//			@Override
//			public void actionPerformed(ActionEvent arg0) {
//				state = STATE_MODIFY;
//				centerCardLayout.show(centerContext, "modify");
//				modifyAction();
//			}
//		});
		
		buttonOK.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				OKAction();
			}
		});
		
		buttonCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				cancelAction();
			}
		});
	}

	private void cancelAction() {
		if(state == STATE_ADD){
			cancelActionByAdd();
		}else if(state == STATE_MODIFY){
			cancelActionByModify();
		}
	}

	private void OKAction() {
		if(state == STATE_ADD){
			OKActionByAdd();
		}else if(state == STATE_MODIFY){
			OKActionByModify();
		}
	}
	
	

	protected abstract void cancelActionByAdd();
	protected abstract void cancelActionByModify();
	protected abstract void OKActionByModify();
	protected abstract void OKActionByAdd();
	protected abstract void deleteAction();
	protected abstract void modifyAction();
	protected abstract void viewAction();
	protected abstract void addAction();
	protected abstract void update();

}
